package com.syni.mdd.sjb.common.datatreaure.controller.protect;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.dto.mddWechatOa.apiCropBusiness.GetEnterpriseCustomersDto;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserChannel;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckUserIdentity;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddWechatOa.CropBusinessRestService;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.customer.EnterpriseWechatRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.customer.EnterpriseWechatVo;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserChannelService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/customer")
public class CustomerDataController {

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    @Autowired
    CropBusinessRestService cropBusinessRestService;

    private final static Map<Integer,String> ADD_WAY_MAP = new HashMap<>();

    static {
        ADD_WAY_MAP.put(0,"未知来源");
        ADD_WAY_MAP.put(1,"扫描二维码");
        ADD_WAY_MAP.put(2,"搜索手机号");
        ADD_WAY_MAP.put(3,"名片分享");
        ADD_WAY_MAP.put(4,"群聊");
        ADD_WAY_MAP.put(5,"手机通讯录");
        ADD_WAY_MAP.put(6,"微信联系人");
        ADD_WAY_MAP.put(7,"来自微信的添加好友申请");
        ADD_WAY_MAP.put(8,"安装第三方应用时自动添加的客服人员");
        ADD_WAY_MAP.put(9,"搜索邮箱");
        ADD_WAY_MAP.put(201,"内部成员共享");
        ADD_WAY_MAP.put(202,"管理员/负责人分配");
    }

    @PostMapping("/enterpriseWechat")
    @ApiOperation(value = "获取企业微信授权数据")
    @CheckUserIdentity
    public Map<String,Object> enterpriseWechat(@RequestBody EnterpriseWechatVo baseRqVo){
        Integer userId = baseRqVo.getUserId();
        EnterpriseWechatRpVo enterpriseWechatRpVo = new EnterpriseWechatRpVo();
        Map<String,Object> userData = new HashMap<>();
        userData.put("pageSize",baseRqVo.getPageSize());
        userData.put("pageNum",baseRqVo.getPageNum());
        userData.put("count",0L);
        //查出当前商家认领的所有店铺
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserChannel.USER_ID,userId);
        List<DxshAppUserChannel> userChannelList = dxshAppUserChannelService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(userChannelList)){
            Set<Integer> businessAuthIds = userChannelList.stream().map(DxshAppUserChannel::getBusinessAuthId).collect(Collectors.toSet());
            if(businessAuthIds.size() > 0) {
                GetEnterpriseCustomersDto getEnterpriseCustomersDto = new GetEnterpriseCustomersDto();
                getEnterpriseCustomersDto.setPageNum(baseRqVo.getPageNum());
                getEnterpriseCustomersDto.setPageSize(baseRqVo.getPageSize());
                getEnterpriseCustomersDto.setBusinessAuthIds(businessAuthIds);
                getEnterpriseCustomersDto.setName(baseRqVo.getName());
                RpcResponseVo rpcResponseVo = cropBusinessRestService.getEnterpriseCustomers(getEnterpriseCustomersDto);
                if(rpcResponseVo.isSuccess()){
                    getEnterpriseCustomersDto = rpcResponseVo.parseObject(GetEnterpriseCustomersDto.class);
                    enterpriseWechatRpVo.setCustomers(BeanUtils.copyList(getEnterpriseCustomersDto.getCustomers(), EnterpriseWechatRpVo.Customers.class));
                    Map<Integer, GetEnterpriseCustomersDto.Customers> customersMap = new HashMap<>();
                    for(GetEnterpriseCustomersDto.Customers customers : getEnterpriseCustomersDto.getCustomers()){
                        customersMap.put(customers.getId(),customers);
                    }
                    enterpriseWechatRpVo.setIsBindEnterpriseWechat(getEnterpriseCustomersDto.getIsBindEnterpriseWechat());
                    for(EnterpriseWechatRpVo.Customers customers : enterpriseWechatRpVo.getCustomers()){
                        if(customersMap.containsKey(customers.getId())){
                            if(ADD_WAY_MAP.containsKey(customersMap.get(customers.getId()).getAddWay())){
                                customers.setAddWay(ADD_WAY_MAP.get(customersMap.get(customers.getId()).getAddWay()));
                            }
                        }
                    }
                    userData.put("pageSize",getEnterpriseCustomersDto.getPageSize());
                    userData.put("pageNum",getEnterpriseCustomersDto.getPageNum());
                    userData.put("count",getEnterpriseCustomersDto.getCount());
                }
            }
        }
        return ResultMapHelper.success(enterpriseWechatRpVo,userData, ResultMsg.MSG_GET_SUCCESS);
    }

}
